# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : zsj1.py.py
# @Time     : 2024/4/12 22:38
# @Author   : Robot-Zsj
"""
description:

i:
4
3
1 2
1 3
1 4
1 5

o:
5
"""
n = int(input())
t = int(input())
wos = [list(map(int, input().split())) for i in range(n)]


def getResult():
    wos.sort(key=lambda x: x[0])

    pq = []

    ans = 0

    curTime = 0

    for wo in wos:
        endTime, score = wo

        if curTime < endTime:
            pq.append(score)
            ans += score
            curTime += 1
        else:
            pq.sort()
            min_score = pq[0]

            if score > min_score:
                pq.pop(0)
                pq.append(score)
                ans += score - min_score

    pq.sort()
    while len(pq) > t:
        ans -= pq.pop(0)

    return ans


print(getResult())
